package cn.cvs.servlet.storagerecord;

import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.StorageRecordService;
import cn.cvs.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "UpdateStorageServlet", urlPatterns = "/sys/storageRecord/update")
public class UpdateStorageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        StorageRecordService storageRecordService= (StorageRecordService) SpringUtil.getBean ( "stoageRecordService" );
        StorageRecord storageRecord= new StorageRecord ();
        String id = request.getParameter ( "id" );
        String srCode = request.getParameter ( "srCode" );
        String goodsName = request.getParameter ( "goodsName" );
        String goodsUnit = request.getParameter ( "goodsUnit" );
        String goodsCount = request.getParameter ( "goodsCount" );
        String totalAmount = request.getParameter ( "totalAmount" );
        String supplierId = request.getParameter ( "supplierId" );
        String payStatus = request.getParameter ( "payStatus" );
        storageRecord.setId ( Integer.valueOf ( id!=null?id:"0" ) );
        storageRecord.setSrCode ( srCode );
        storageRecord.setGoodsName ( goodsName );
        storageRecord.setGoodsUnit ( goodsUnit );
        storageRecord.setGoodsCount ( Double.parseDouble ( goodsCount ) );
        storageRecord.setTotalAmount ( Double.parseDouble ( totalAmount ) );
        storageRecord.setSupplierId ( Integer.valueOf ( supplierId ) );
        storageRecord.setPayStatus ( Integer.valueOf ( payStatus ) );
        SysUser userSession = (SysUser) request.getSession ( ).getAttribute ( "userSession" );
        storageRecord.setUpdatedUserId ( userSession.getId() );
        storageRecord.setUpdatedTime ( new Date (  ) );


        int update = storageRecordService.update ( storageRecord );
        if (update>0){
            request.getRequestDispatcher ( "/sys/storageRecord/list" ).forward ( request,response );
        }else {
            request.getRequestDispatcher ( "/sys/storageRecord/toUpdate" ).forward ( request,response );
        }
    }
}
